package com.example.yang.yixin.Activity;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.yang.yixin.FragmentManager.Fragment_All;
import com.example.yang.yixin.FragmentManager.Fragment_Earlyorlate;
import com.example.yang.yixin.FragmentManager.Fragment_person;
import com.example.yang.yixin.FragmentManager.Fragment_phonecontrol;
import com.example.yang.yixin.R;
import com.example.yang.yixin.Service.RefreshService;
import com.example.yang.yixin.Utils.ScreenLock;
import com.example.yang.yixin.Views.AdminReceiver;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class BankeActivity extends BaseActivity{
View v;
TextView textView;
SharedPreferences pref;
SharedPreferences.Editor editor;
    private ImageView mIv_back;
    private ImageView mIv_caidan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_banke);
        mIv_back=findViewById(R.id.iv_back);
        mIv_caidan=findViewById(R.id.iv_caidan);
        mIv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(BankeActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
        registerForContextMenu( mIv_caidan);
        mIv_caidan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openContextMenu(v);
            }
        });
        initView();






    }




    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(1, 1, 1, "文件上传");
        menu.add(1, 2, 2, "视频上传");


    }
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case 1:

                break;
            case 2:

                break;
        }
        return true;
    }
    private void initView() {
        initBottombar(v);
        pref=getSharedPreferences("user",MODE_PRIVATE);
        String username=getIntent().getStringExtra("data");
        String bkpassword=getIntent().getStringExtra("bkpassword");
        editor= pref.edit();
        editor.putString("bkpassword",bkpassword);//已经存放过bkpassword
        editor.putString("username",username);//存放姓名
        editor.apply();
        Intent intent=new Intent();
        intent.setClass(BankeActivity.this,RefreshService.class);
        startService(intent);


    }
    private void getServerData(String bkpassword) {
        String jsonurl = "http://47.106.141.161/MyFirstAndroidApp/InformationServlet?bkpassword="+bkpassword;
        RequestQueue mQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonurl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {


                    String cname=response.getString("cname");
                    String classname=response.getString("classname");
                    String requested=response.getString("requested");
                    String sign=response.getString("sign");
                    String phone=response.getString("phone");
                    editor=pref.edit();
                    editor.putString("classname",classname);
                    editor.putString("requested",requested);
                    editor.putString("sign",sign);
                    editor.putString("phone",phone);
                    editor.putString("cname",cname);
                    editor.apply();

                } catch (JSONException e) {

                    Snackbar.make(getWindow().getDecorView(),"网络出现了一些故障！",Snackbar.LENGTH_SHORT).setAction("我知道了", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    });

                }

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("TAG错误", volleyError.getMessage(), volleyError);
                    }
                }) {
            protected Response<JSONObject> paraeNetworkResponse(NetworkResponse response) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(response.data, "utf-8"));
                    return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
                }catch (JSONException je) {
                    return Response.error(new ParseError(je));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                }

            }
        };


        mQueue.add(jsonObjectRequest);
    }


}
